﻿namespace Amazon.Checkout.Settings
{
    using System.Collections.Generic;
    using System.Linq;

    public class CheckoutByAmazonButtonPartSettings
    {
        private string productTitleMapping;
        private string productDescriptionMapping;
        private string productPriceMapping;
        private string productSkuMapping;
        private string productImageUrlMapping;

        public string ExpressCheckout { get; set; }

        public bool ShowWarning { get; set; }

        public string ProductTitleMapping
        {
            get { return string.IsNullOrEmpty(this.productTitleMapping) && this.PossibleMappings != null ? this.FieldContaining("Title") : this.productTitleMapping; }
            set { this.productTitleMapping = value; }
        }

        public string ProductPriceMapping
        {
            get { return string.IsNullOrEmpty(this.productPriceMapping) && this.PossibleMappings != null ? this.FieldContaining("Price") : this.productPriceMapping; }
            set { this.productPriceMapping = value; }
        }

        public string ProductDescriptionMapping
        {
            get { return string.IsNullOrEmpty(this.productDescriptionMapping) && this.PossibleMappings != null ? this.FieldContaining("Description") : this.productDescriptionMapping; }
            set { this.productDescriptionMapping = value; }
        }

        public string ProductSkuMapping
        {
            get { return string.IsNullOrEmpty(this.productSkuMapping) && this.PossibleMappings != null ? this.FieldContaining("Sku") : this.productSkuMapping; }
            set { this.productSkuMapping = value; }
        }

        public string ProductImageUrlMapping
        {
            get { return string.IsNullOrEmpty(this.productImageUrlMapping) && this.PossibleMappings != null ? this.FieldContaining("ImageUrl") : this.productImageUrlMapping; }
            set { this.productImageUrlMapping = value; }
        }

        public IEnumerable<string> PossibleMappings { get; set; }

        public IEnumerable<string> GetPossibleMappings()
        {
            return this.PossibleMappings;
        }

        public void SetEmptyFieldsToDefaults()
        {
            if (string.IsNullOrEmpty(this.ProductTitleMapping))
            {
                this.ProductTitleMapping = this.FieldContaining("Title");
            }

            if (string.IsNullOrEmpty(this.ProductDescriptionMapping))
            {
                this.ProductDescriptionMapping = this.FieldContaining("Description");
            }

            if (string.IsNullOrEmpty(this.ProductSkuMapping))
            {
                this.ProductSkuMapping = this.FieldContaining("Sku");
            }

            if (string.IsNullOrEmpty(this.ProductPriceMapping))
            {
                this.ProductPriceMapping = this.FieldContaining("Price");
            }

            if (string.IsNullOrEmpty(this.ProductImageUrlMapping))
            {
                this.ProductImageUrlMapping = this.FieldContaining("ImageUrl");
            }
        }

        private string FieldContaining(string argument)
        {
            foreach (var mapping in this.PossibleMappings.Where(mapping => mapping.ToLower().Contains(argument.ToLower())))
            {
                return mapping;
            }

            return string.Empty;
        }
    }
}
